<?php


namespace app\service;

use app\model\ModelAppNews;
use app\model\ModelAppNotice;
use app\model\ModelUserAccountCard;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 新闻 公告 服务类
 * Class NewsService
 * @package app\service
 */
class NewsService extends BaseService
{
    /**
     * id 获取新闻信息
     * @param int $id
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNewsById(int $id): array
    {
        $appNewsModel = new ModelAppNews();
        $res = $appNewsModel->findOrEmpty($id) -> toArray();
        return $this->success('获取成功',$res);
    }

    /**
     * 获取获取系统公告记录--支持动态分页
     * @param int $page 获取第几页的数据
     * @param int $size 分页尺寸;只支持，5,10,20分页尺寸
     * @return array 返回推荐任务的数组
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getNoticeListForPage(int $page, int $size = 10): array
    {
        if ($size < 10) {
            $size = 10;
        }
        if($size != 10 && $size != 20 && $size != 5){
            $size = 10;
        }
        if ($page <= 0) {
            $page = 0;
        }else{
            if($page > 50){
                $page = 50;
            }
        }
        $noticeModel = new ModelAppNotice();
        $list = $noticeModel
            ->field('*')
            ->page($page,$size)
            -> select();
        //dump($page,$size,$userModel -> getLastSql());die();
        return $this->success('成功', $list);
    }

    /**
     * 获取公告信息--通过信息ID
     * @param int $id 公告ID
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNoticeById(int $id): array
    {
        $appNoticesModel = new ModelAppNotice();
        $res = $appNoticesModel-> findOrEmpty($id) ->toArray();
        return $this->success('获取成功',$res);
    }

}